package com.example.demo20;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class ExMainActivity extends AppCompatActivity {
    private ExpandableListView listView;
    private MyExListViewAdapter myAdapter;
    private int[] groupPhoto;
    private String[] groupName;
    private int[][] childPhoto;
    private String[][] childName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex_main);
        listView = findViewById(R.id.ex_notes);
        groupPhoto = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
        groupName = new String[]{"平行线", "同学", "老师", "黑名单"};
        childPhoto = new int[][]{{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher},
                {R.mipmap.ic_launcher,R.mipmap.ic_launcher},
                {R.mipmap.ic_launcher,R.mipmap.ic_launcher},
                {R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}};
        childName=new String[][]{{"老大","老二","老三"},{"想想","九九"},{"恭恭","喜喜"},{"发发","踩踩","好好","好好"}};
        myAdapter=new MyExListViewAdapter(this,groupPhoto,groupName,childPhoto,childName);
        listView.setAdapter(myAdapter);
        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(),myAdapter.getGroup(groupPosition).toString()+"de"+myAdapter.getChild(groupPosition,childPosition).toString(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }


}

class MyExListViewAdapter extends BaseExpandableListAdapter {
    private int[] groupPhoto;
    private String[] groupName;
    private int[][] childPhoto;
    private String[][] childName;
    LayoutInflater inflater;

    public MyExListViewAdapter(Context context,int[] groupPhoto, String[] groupName, int[][] childPhoto, String[][] childName) {
        this.groupPhoto = groupPhoto;
        this.groupName = groupName;
        this.childPhoto = childPhoto;
        this.childName = childName;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupName.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childName[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupName[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childName[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //绑定分组布局
        convertView=inflater.inflate(R.layout.ex_notes_group_layout,null);
        //注册控件
        ImageView photo=convertView.findViewById(R.id.ex_notes_group_photo);
        TextView name=convertView.findViewById(R.id.ex_notes_group_name);
        //设值
        name.setText(getGroup(groupPosition).toString());
        photo.setImageResource(groupPhoto[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView=inflater.inflate(R.layout.ex_notes_child_layout,null);
        ImageView photo=convertView.findViewById(R.id.ex_notes_child_photo);
        TextView name=convertView.findViewById(R.id.ex_notes_child_name);
        photo.setImageResource(childPhoto[groupPosition][childPosition]);
        name.setText(getChild(groupPosition,childPosition).toString());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}